package mpcs
{	
	import flash.events.Event;
	
	import flexlib.mdi.containers.MDIWindow;
	
	import mpcs.model.vo.GlobalVO;
	import mpcs.model.vo.UserVO;
	import mpcs.mylibs.manager.ConManager;
	import mpcs.utils.net.NetMonitor;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 *  
	 * @author zhangzuoqiang
	 */
	public class MainMediator extends Mediator {
		
		public static var NAME:String = "mainMediator";
		
		private var preferences:XML; 
		
		private var mapMDIWin:MDIWindow;
		
		public function MainMediator(viewComponent:MainApp) {
			super(NAME, viewComponent);
		}
		
		private function get view():MainApp {
			return viewComponent as MainApp;
		}
		
		override public function listNotificationInterests() : Array {
			return [
				NotiConst.STARTUP,
				NotiConst.NET_CONNECT_SUCCESS,
				NotiConst.NET_CONNECT_FAILED
			];
		}
		
		override public function handleNotification(notification:INotification) : void {
			var name:String = notification.getName();
			switch(name){
				case NotiConst.NET_CONNECT_SUCCESS:
					trace("网络连接良好，开始发送登录请求 ... ");
					loginHandle();
					break;
				case NotiConst.NET_CONNECT_FAILED:
					connFailedHandle();
					break;
				case NotiConst.STARTUP:
					GlobalVO.initPreferencesXML();//开启程序，登陆框显示配置
					break;
			}
		}
		
		// 网络连接成功才发送登录请求
		private function loginHandle():void {
			
			// 如果用户已经登录
			if( GlobalVO.isLogin ){
				trace("MainMediator  loginHandle  .....  ");
				return;
			}
			
			if ( view.loginForm.email.text == ""){
				view.loginForm.emailinputColor = "0xFF0000";
				view.loginForm.email.setFocus();
				return;
			}else if(view.loginForm.password.text == ""){
				view.loginForm.passinputColor  = "0xFF0000";
				view.loginForm.password.setFocus();
				return;
			}else{
				var uservo:UserVO = new UserVO(view.loginForm.email.text, view.loginForm.password.text);
				sendNotification( NotiConst.S_LOGIN, uservo);
				view.loginForm.loginBtn.enabled = false;
			}
		}
		
		/**网络连接故障处理**/
		private function connFailedHandle():void {
			if(NetMonitor.MonitorCount == 0)
				return;
			trace("无法连接网络 ... ");
			// 弹出提示框，当前无法连接网络
			Alert.show("网络连接异常, 请检查您的网络。", "提示");
			GlobalVO.isLogin = false;
		}
		
	}
}